package com.besall.allbase.view.activity.chipstoollevel4.protocol.Settings;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import bb.lanxing.R;
import bb.lanxing.utils.PanelItemTypeUtil;

public class DevicePanelSettingAdapter extends RecyclerView.Adapter<DevicePanelSettingAdapter.ViewHolder> {
    private List<Integer> mPanelItemTypeList;
    private int size;
    private int idSelected = -1;
    private OnItemClickListener listener;
    private Context mContext;

    public DevicePanelSettingAdapter(List<Integer> panelItemTypeList) {
        mPanelItemTypeList = panelItemTypeList;
        size = panelItemTypeList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView sport_item_digital;
        TextView sport_item_digital_unit;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            sport_item_digital = itemView.findViewById(R.id.sport_item_digital);
            sport_item_digital_unit = itemView.findViewById(R.id.sport_item_digital_unit);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_device_panel, parent, false);
        if (size < 4) {
            ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
            float density = mContext.getResources().getDisplayMetrics().density;
            int height = (int) (layoutParams.height + 2 * density);
            if (size == 1) {
                layoutParams.height = height * 4;
            } else if (size == 2) {
                layoutParams.height = height * 2;
            } else if (size == 3) {
                layoutParams.height = height * 4 / 3;
            }
            view.setLayoutParams(layoutParams);
        }
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if (idSelected == position) {
            holder.itemView.setForeground(mContext.getDrawable(R.drawable.item_selected_fg));
        } else {
            holder.itemView.setForeground(mContext.getDrawable(R.drawable.sport_item_edit_state));
        }
        holder.sport_item_digital.setText(PanelItemTypeUtil.resetDeviceDefault(mPanelItemTypeList.get(position)));
        holder.sport_item_digital_unit.setText(PanelItemTypeUtil.getDeviceItemName(mPanelItemTypeList.get(position)));

        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onItemClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mPanelItemTypeList.size();
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public void setIdSelected(int idSelected) {
        this.idSelected = idSelected;
        notifyDataSetChanged();
    }

    public void setPanelItemTypeList(List<Integer> mPanelItemTypeList) {
        this.mPanelItemTypeList = mPanelItemTypeList;
        notifyDataSetChanged();
    }
}